package GUI;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Other.Glob;

import database.DatabaseExtension;
import database.MyCountry;
import database.MyIncoterm;
import database.MyPayment;
import database.MyTax;
import database.MyClient;
import database.MySupplier;
import database.MyAddressee;
import database.MyDestination;
import database.NewTirDataBase;
import enums.DialogType;
import enums.SearchType;


public class ScreenHandling {

	public static boolean checkIdStringOnTable(JTextField textfield, SearchType type) throws Exception{
		
		ArrayList<String> id = new ArrayList<String>();
		boolean Ok = false;
		
		if(type == SearchType.TRANSPORT){
			id = DatabaseExtension.columnIDsString("Select * from transport");
		}
	
	for (String i: id) {
		if (i.equalsIgnoreCase(textfield.getText()))
				Ok=true;
	}		
		if (Ok) {
		
			//do nothing
		
	}else{
		
		JOptionPane.showMessageDialog(null, "ShipmentID is not in the database");
		textfield.setText("");
		textfield.requestFocus();
		throw new Exception();
		
		}
		
		return Ok;
		
	}
	
	public static boolean checkIdOnTable(JTextField textfield, SearchType type) throws Exception{
		String query = null;
		String tableName = null;
		
		if (type == SearchType.COUNTRY){
			tableName = "This ID is not on the Country table";
			query ="select * from country";
		}else if (type == SearchType.PAYMENT){
			tableName = "This ID is not on the Payment table";
			query ="select * from payment";
		}else if (type == SearchType.TAX){
			tableName = "This ID is not on the Tax table";
			query ="select * from tax";
		}else if (type == SearchType.ADDRESSEE){
			tableName = "This ID is not on the Addressee table";
			query ="select * from addressee";
		}else if (type == SearchType.CLIENT){
			tableName = "This ID is not on the Client table";
			query ="select * from client";
		}else if (type == SearchType.DESTINATION){
			tableName = "This ID is not on the Destination table";
			query ="select * from destination";
		}else if (type == SearchType.INCOTERM){
			tableName = "This ID is not on the Incoterm table";
			query ="select * from incoterm";
		}else if (type == SearchType.SHIPMENT){
			tableName = "This ID is not on the Shipment table";
			query ="select * from shipment";
		}else if (type == SearchType.SUPPLIER){
			tableName = "This ID is not on the Supplier table";
			query ="select * from supplier";
		}
		
		boolean ok = false;
		
		ArrayList<Integer> ids = DatabaseExtension.columnIDs(query);
	
		
		try{
		if(textfield.getText().trim().length()<1) {
		//Do nothing
		}else{
			if(textfield.getText().trim().length()>1) {
				if(Integer.parseInt(textfield.getText())<=0){
					JOptionPane.showMessageDialog(null, "Code must be a positive number");
					textfield.setText("");
					textfield.requestFocus();
					throw new Exception();
					
				}
				}
			
			
		for (int i: ids) {
			if (i == Integer.parseInt(textfield.getText()))
					ok=true;
		}	
		
	
		
			if (ok == false) {
			JOptionPane.showMessageDialog(null, tableName);
			textfield.requestFocus();
			textfield.setText("");
			throw new Exception();
			}
			
		}
		}catch(NumberFormatException alpha){
			alpha.printStackTrace();
			JOptionPane.showMessageDialog(null, "Code must be a valid positive number");
			textfield.requestFocus();
			textfield.setText("");
			throw new Exception();
		}
		return ok;
	}
	
	
	public static void enterData(JTextField code, JTextField name, JTextField focus, SearchType type){
		try{
			//set up global variable for the country screen
			Glob.type = type;
			
			checkIdOnTable(code, type);
			try{
				if(code.getText().trim().length()>1) {
				if(Integer.parseInt(code.getText())<=0){
					JOptionPane.showMessageDialog(null, "Code has to be a valid number");
					code.setText("");
					code.requestFocus();
					return;
					
				}
				}
				}catch(NumberFormatException n){
					
					JOptionPane.showMessageDialog(null, "Code number is to big");
					code.setText("");
					code.requestFocus();
					return;
					
				}
			if(Integer.parseInt(code.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.setText("");
				focus.requestFocus();
				return;
			}
			
			String tableName = null;
			String tableID = null;
			
			if (type == SearchType.COUNTRY){
				NewTirDataBase c = (MyCountry) DatabaseExtension.getTableID( "SELECT * FROM Country WHERE CountryID = '" + code.getText() +"'")[0];
				name.setText(((MyCountry) c).getCountryName());
			}else if (type == SearchType.PAYMENT){
				NewTirDataBase c = (MyPayment) DatabaseExtension.getTableID( "SELECT * FROM Payment WHERE PaymentID = '" + code.getText() +"'")[0];
				name.setText(((MyPayment) c).getPaymentName());
			}else if (type == SearchType.INCOTERM){
				NewTirDataBase c = (MyIncoterm) DatabaseExtension.getTableID( "SELECT * FROM Incoterm WHERE IncotermID = '" + code.getText() +"'")[0];
				name.setText(((MyIncoterm) c).getIncotermName());
			}else if (type == SearchType.TAX){
				NewTirDataBase c = (MyTax) DatabaseExtension.getTableID( "SELECT * FROM Tax WHERE TaxID = '" + code.getText() +"'")[0];
				name.setText(((MyTax) c).getTaxName());
			}else if (type == SearchType.CLIENT){
				NewTirDataBase c = (MyClient) DatabaseExtension.getTableID( "SELECT * FROM client WHERE clientID = '" + code.getText() +"'")[0];
				name.setText(((MyClient) c).getName());
			}else if (type == SearchType.ADDRESSEE){
				NewTirDataBase c = (MyAddressee) DatabaseExtension.getTableID( "SELECT * FROM addressee WHERE addresseeID = '" + code.getText() +"'")[0];
				name.setText(((MyAddressee) c).getName());
			}else if (type == SearchType.DESTINATION){
				NewTirDataBase c = (MyDestination) DatabaseExtension.getTableID( "SELECT * FROM destination WHERE destinationID = '" + code.getText() +"'")[0];
				name.setText(((MyDestination) c).getName());
			}else if (type == SearchType.INCOTERM){
				NewTirDataBase c = (MyIncoterm) DatabaseExtension.getTableID( "SELECT * FROM incoterm WHERE incotermID = '" + code.getText() +"'")[0];
				name.setText(((MyIncoterm) c).getIncotermName());
			}else if (type == SearchType.SUPPLIER){
				NewTirDataBase c = (MySupplier) DatabaseExtension.getTableID( "SELECT * FROM supplier WHERE supplierID = '" + code.getText() +"'")[0];
				name.setText(((MySupplier) c).getName());
			}
			
			
			
	
		//System.out.println("this is the country id and name" + c.getCountryID() + c.getCountryName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		focus.requestFocus();
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to empty
			name.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			focus.requestFocus();
		}catch(NumberFormatException exception){
			//if code tf is empty
			if(code.getText().trim().length()<1){
				focus.requestFocus();
				return;
			}else{
				//set code tf to null
				code.setText(null);
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				name.setText("");
				code.requestFocus();
			} 
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	
	public static void enterDataArea(JTextField code, JTextField name, JTextArea focus, SearchType type){
		try{
			//set up global variable for the country screen
			Glob.type = type;
			
			checkIdOnTable(code, type);
			try{
				if(code.getText().trim().length()>1) {
				if(Integer.parseInt(code.getText())<=0){
					JOptionPane.showMessageDialog(null, "Code has to be a valid number");
					code.setText("");
					code.requestFocus();
					return;
					
				}
				}
				}catch(NumberFormatException n){
					
					JOptionPane.showMessageDialog(null, "Code number is to big");
					code.setText("");
					code.requestFocus();
					return;
					
				}
			if(Integer.parseInt(code.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.setText("");
				focus.requestFocus();
				return;
			}
			
			String tableName = null;
			String tableID = null;
			
			if (type == SearchType.COUNTRY){
				NewTirDataBase c = (MyCountry) DatabaseExtension.getTableID( "SELECT * FROM Country WHERE CountryID = '" + code.getText() +"'")[0];
				name.setText(((MyCountry) c).getCountryName());
			}else if (type == SearchType.PAYMENT){
				NewTirDataBase c = (MyPayment) DatabaseExtension.getTableID( "SELECT * FROM Payment WHERE PaymentID = '" + code.getText() +"'")[0];
				name.setText(((MyPayment) c).getPaymentName());
			}else if (type == SearchType.INCOTERM){
				NewTirDataBase c = (MyIncoterm) DatabaseExtension.getTableID( "SELECT * FROM Incoterm WHERE IncotermID = '" + code.getText() +"'")[0];
				name.setText(((MyIncoterm) c).getIncotermName());
			}else if (type == SearchType.TAX){
				NewTirDataBase c = (MyTax) DatabaseExtension.getTableID( "SELECT * FROM Tax WHERE TaxID = '" + code.getText() +"'")[0];
				name.setText(((MyTax) c).getTaxName());
			}else if (type == SearchType.CLIENT){
				NewTirDataBase c = (MyClient) DatabaseExtension.getTableID( "SELECT * FROM client WHERE clientID = '" + code.getText() +"'")[0];
				name.setText(((MyClient) c).getName());
			}else if (type == SearchType.ADDRESSEE){
				NewTirDataBase c = (MyAddressee) DatabaseExtension.getTableID( "SELECT * FROM addressee WHERE addresseeID = '" + code.getText() +"'")[0];
				name.setText(((MyAddressee) c).getName());
			}else if (type == SearchType.DESTINATION){
				NewTirDataBase c = (MyDestination) DatabaseExtension.getTableID( "SELECT * FROM destination WHERE destinationID = '" + code.getText() +"'")[0];
				name.setText(((MyDestination) c).getName());
			}else if (type == SearchType.INCOTERM){
				NewTirDataBase c = (MyIncoterm) DatabaseExtension.getTableID( "SELECT * FROM incoterm WHERE incotermID = '" + code.getText() +"'")[0];
				name.setText(((MyIncoterm) c).getIncotermName());
			}
			
			
	
		//System.out.println("this is the country id and name" + c.getCountryID() + c.getCountryName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		focus.requestFocus();
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to empty
			name.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			focus.requestFocus();
		}catch(NumberFormatException exception){
			//if code tf is empty
			if(code.getText().trim().length()<1){
				focus.requestFocus();
				return;
			}else{
				//set code tf to null
				code.setText(null);
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				name.setText("");
				code.requestFocus();
			} 
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	
	public static void remove(JTextField code, SearchType type){
		//set up global variable for this screen
		Glob.type = type;
		//set up global variable for deleting an item
		Glob.dialog= DialogType.DELETE;
		
		ArrayList<String> id = null;
		ArrayList<Integer> ids = null;
		
		if(type == SearchType.TRANSPORT){
			
			id = new ArrayList<String>();
		}else{
			
			ids = new ArrayList<Integer>();
			
			if(Integer.parseInt(code.getText()) <= 0) {
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.setText("");
				code.requestFocus();
				return;
			}
		}
		
		
		try{
			
	
	
		
	if (type == SearchType.COUNTRY){
		ids = DatabaseExtension.columnIDs("Select * from Country");
	}else if (type == SearchType.PAYMENT){
		  ids = DatabaseExtension.columnIDs("Select * from Payment");
	}else if (type == SearchType.INCOTERM){
		ids = DatabaseExtension.columnIDs("Select * from Incoterm");
	}else if (type == SearchType.TAX){
		ids = DatabaseExtension.columnIDs("Select * from Tax");
	}else if (type == SearchType.ADDRESSEE){
		ids = DatabaseExtension.columnIDs("Select * from Addressee");
	}else if (type == SearchType.SUPPLIER){
		ids = DatabaseExtension.columnIDs("Select * from Supplier");
	}else if (type == SearchType.CLIENT){
		ids = DatabaseExtension.columnIDs("Select * from client");
	}else if (type == SearchType.DESTINATION){
		ids = DatabaseExtension.columnIDs("Select * from destination");
	}else if (type == SearchType.SHIPMENT){
		ids = DatabaseExtension.columnIDs("Select * from shipment");
	}else if (type == SearchType.GOOD){
		ids = DatabaseExtension.columnIDs("Select * from good");
	}else if (type == SearchType.TRANSPORT){
		id = DatabaseExtension.columnIDsString("Select * from transport");
	}
	
	//enter condition right here
	
	boolean ok = false;
	
	if(type == SearchType.TRANSPORT){
		
		for (String i: id) {
			if (i.equalsIgnoreCase(code.getText()))
					ok=true;
		}
			if (!ok) {
			JOptionPane.showMessageDialog(null, "This ID is not on the table");
			code.setText("");
			code.requestFocus();
			return;
		}
		
	}else{
		
		for (int i: ids) {
			if (i == Integer.parseInt(code.getText()))
					ok=true;
		}
			if (!ok) {
			JOptionPane.showMessageDialog(null, "This ID is not on the table");
			code.setText("");
			code.requestFocus();
			return;
		}
		
	}
		
			
			
			Dialog d = Dialog.getInstance();
			d.setVisible(true);
			
			if (type == SearchType.COUNTRY){
				Country c = Country.getInstance();
				c.disableCountry();
			}else if (type == SearchType.PAYMENT){
				Payment p = Payment.getInstance();
				p.disablePayment();
			}else if (type == SearchType.INCOTERM){
				Incoterm i = Incoterm.getInstance();
				i.disableIncoterm();
			}else if (type == SearchType.TAX){
				Tax t = Tax.getInstance();
				t.disableTax();
			}else if (type == SearchType.ADDRESSEE){
				Addressee a = Addressee.getInstance();
				a.disableAddressee();
			}else if (type == SearchType.SUPPLIER){
				Supplier s = Supplier.getInstance();
				s.disableSupplier();
			}else if (type == SearchType.CLIENT){
				Client c = Client.getInstance();
				c.disableClient();			
			}else if (type == SearchType.DESTINATION){
				Destination de = Destination.getInstance();
				de.disableDestination();			
			}else if (type == SearchType.SHIPMENT){
				Shipment shipment = Shipment.getInstance();
				shipment.disableShipment();			
			}else if (type == SearchType.GOOD){
				// Do nothing in this case
			}else if (type == SearchType.TRANSPORT){
				Transport transport = Transport.getInstance();
				transport.disableTransport();		
			}
			//enter condition right here
			}catch(Exception z){
				JOptionPane.showMessageDialog(null, "Code may not be empty and must have a proper ID");
				code.requestFocus();
				code.setText(null);
				z.printStackTrace();
			}
	}
	
	
	public static void checkIDisValid(JTextField code) throws Exception{
		try{
			
			if(code.getText().isEmpty()) {
			if(Integer.parseInt(code.getText())<=0){
				JOptionPane.showMessageDialog(null, "Code has to be a valid positive number");
				code.setText("");
				code.requestFocus();
				return;
				
			}
			}
			}catch(NumberFormatException n){
				
				JOptionPane.showMessageDialog(null, "Code has to be a valid number");
				code.setText("");
				code.requestFocus();
				return;
				
			}
	
}
	
	public static void checkIDString(JTextField code) throws Exception{
		
		
		
			if(code.getText().isEmpty()){
				JOptionPane.showMessageDialog(null, "Code may not be empty");
				code.setText("");
				code.requestFocus();
				throw new Exception();
			}
		
	}
	
	public static void checkID(JTextField code) throws Exception{
		
		
		try{
			if(code.getText().isEmpty()){
				JOptionPane.showMessageDialog(null, "Code may not be empty");
				code.setText("");
				code.requestFocus();
				throw new Exception();
			}
			else if(Integer.parseInt(code.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				code.setText("");
				code.requestFocus();
				throw new Exception();
			}
		}catch (NumberFormatException number){
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			code.setText("");
			code.requestFocus();
			throw new Exception();
		}
	}

	
}
